﻿using Cart.API;
using Cart.Bll;
using Cart.model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;

namespace Cart.Api.Controllers
{
    public class UsersController : ApiController
    {
        UserBll bll = new UserBll();
        // GET: api/Users
       
        public ResultPageList<UsersDto> Get(int page, int limit, string name="")
        {
            int count;
            var list = bll.pagelist(page, limit, name, out count);

            ResultPageList<UsersDto> re = new ResultPageList<UsersDto>();



            var data = from u in list
                       select new UsersDto
                       {
                           UserID = u.userID,
                           UserName = u.userName,
                           Sex = u.sex,
                           Birthday = u.birthday,
                           ImgID = u.imgID,
                           Path = u.Images.path,
                           Phone = u.phone,
                           LoginName = u.loginName,
                           LoginPwd = u.loginPwd,
                           Status = u.status,
                           RoleID = u.roleID,
                           RoleName = u.Roles.roleName,
                           DeptID = u.Roles.deptID,
                           DeptName = u.Roles.Department.deptName
                       };

            re.data = data.ToList();
            re.code = 0;
            re.count = count;
            re.msg = data.Count()>0? "ok" : "error";
            return re;

        }

        // GET: api/Users/5

        // GET: api/login/5

        // POST: api/Users
        //添加
        public void Post(Users us)
        {
            Result re = new Result();
            //UsersFormDto uu = new UsersFormDto()
            //{
            //    UserID = us.userID,
            //    userName = us.userName,
            //    birthday = us.birthday,
            //    phone = us.phone,
            //    Images = us.Images,
            //    Roles = us.Roles,
            //    loginName = us.loginName,
            //    imgID = us.imgID,
            //    loginPwd = MD5Helper.Md5(us.loginPwd, "X2"),
            //    roleID = us.roleID,
            //    sex = us.sex,
            //    lastTime=us.lastTime,
            //    status = us.status,
            //};
            //int ol = bll.AddUser(uu);
            //re.code = ol > 0 ? 1 : 0;
            //re.msg = ol > 0 ? "ok" : "error";
        }

        // PUT: api/Users/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/Users/5
        public Result Delete(int id)
        {
            Result re = new Result();
            var bo = bll.det(id);
            re.code = bo > 0 ? 1 : 0;
            re.msg = bo > 0 ? "ok" : "error";
            return re;
        }

        [System.Web.Http.Route("api/Users/AA")]
        public Result DeleteAA(int []ids)
        {
            Result re = new Result();
            var bo = bll.detall(ids);
            re.code = bo > 0 ? 1 : 0;
            re.msg = bo > 0 ? "ok" : "error";
            return re;
        }

    }
}
